package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.vo.Catalog2Vo;
import com.guli.gulimall.product.vo.Catalog3Vo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public HashMap<String, List<Catalog2Vo>> catalogList() {
//        HashMap<String, List<Catalog2Vo>> catalogList = new HashMap<>();
//        List<CategoryEntity> cate1List = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0L));
//        for (CategoryEntity categoryEntity : cate1List) {
//            List<Catalog2Vo> catalog2Vos = getcatalog2List(categoryEntity.getCatId());
//            catalogList.put(categoryEntity.getCatId().toString(),catalog2Vos);
//        }
//        return catalogList;
        HashMap<String, List<Catalog2Vo>> listHashMap = new HashMap<>();
        //查一级菜单
        //List<CategoryEntity> categoryList = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0L));
        //查所有菜单
        List<CategoryEntity> allList = baseMapper.selectList(null);
        List<CategoryEntity> categoryList = findCateLitsByParentId(allList, 0L);
        for (CategoryEntity entity : categoryList) {
            List<Catalog2Vo> catalog2Vos = getcata2List(allList,entity.getCatId());
            listHashMap.put(entity.getCatId().toString(),catalog2Vos);
        }
        return listHashMap;
    }


    private List<Catalog2Vo> getcata2List(List<CategoryEntity> allList,Long catId){
        //List<CategoryEntity> categoryList = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", catId));
        List<CategoryEntity> categoryList = findCateLitsByParentId(allList, catId);
        List<Catalog2Vo> collect = categoryList.stream().map(e -> {
            Catalog2Vo catalog2Vo = new Catalog2Vo();
            catalog2Vo.setCatalog1Id(catId.toString());
            catalog2Vo.setId(e.getCatId().toString());
            catalog2Vo.setName(e.getName());
            List<Catalog3Vo> catalog3Vos = getcata3List(allList,catId);
            catalog2Vo.setCatalog3List(catalog3Vos);
            return catalog2Vo;
        }).collect(Collectors.toList());
        return collect;
    }

    //根据parent_cid查列表
    public List<CategoryEntity> findCateLitsByParentId(List<CategoryEntity> alist,Long catId){
        ArrayList<CategoryEntity> arrayList = new ArrayList<>();
        for (CategoryEntity categoryEntity : alist) {
            if (categoryEntity.getParentCid()==catId){
                arrayList.add(categoryEntity);
            }
        }
        return arrayList;
    }

    private List<Catalog3Vo> getcata3List(List<CategoryEntity> allList,Long catId){
        //List<CategoryEntity> categoryList = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", catId));
        List<CategoryEntity> categoryList = findCateLitsByParentId(allList, catId);
        List<Catalog3Vo> collect = categoryList.stream().map(e -> {
            Catalog3Vo catalog3Vo = new Catalog3Vo();
            catalog3Vo.setId(e.getCatId().toString());
            catalog3Vo.setName(e.getName());
            catalog3Vo.setCatalog2Id(catId.toString());
            return catalog3Vo;
        }).collect(Collectors.toList());
        return collect;
    }
//    private List<Catalog2Vo> getcatalog2List(Long catId){
//        List<CategoryEntity> cata2List = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", catId));
//        List<Catalog2Vo> collect = cata2List.stream().map(e -> {
//            Catalog2Vo catalog2Vo = new Catalog2Vo();
//            catalog2Vo.setCatalog1Id(catId.toString());
//            catalog2Vo.setId(e.getCatId().toString());
//            catalog2Vo.setName(e.getName());
//            List<Catalog3Vo> catalog3Vos = getcatalog3List(e.getCatId());
//            catalog2Vo.setCatalog3List(catalog3Vos);
//            return catalog2Vo;
//        }).collect(Collectors.toList());
//        return collect;
//
//    }
//
//
//    private List<Catalog3Vo> getcatalog3List(Long catId){
//        List<CategoryEntity> cata3List = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", catId));
//        List<Catalog3Vo> collect = cata3List.stream().map(e -> {
//            Catalog3Vo catalog3Vo = new Catalog3Vo();
//            catalog3Vo.setCatalog2Id(catId.toString());
//            catalog3Vo.setId(e.getCatId().toString());
//            catalog3Vo.setName(e.getName());
//            return catalog3Vo;
//        }).collect(Collectors.toList());
//        return collect;
//    }
}